
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaMatriz.java
 *
 * Created on 11/11/2011, 11:40:32 AM
 */
/**
 *
 * @author JSIERRA
 */
public class VentanaMatriz extends javax.swing.JFrame {

    Logica_Matriz mat = new Logica_Matriz();
      String Mos="";
      
    /** Creates new form VentanaMatriz */
    public VentanaMatriz() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        fil = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        col = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        valor = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        mos = new javax.swing.JTextArea();
        guardar = new javax.swing.JButton();
        llenar = new javax.swing.JButton();
        limpiar = new javax.swing.JButton();
        salir = new javax.swing.JButton();
        crear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MATRIZ SIMPLE");

        jLabel1.setFont(new java.awt.Font("Kristen ITC", 0, 14));
        jLabel1.setForeground(new java.awt.Color(153, 0, 153));
        jLabel1.setText("MATRIZ SIMPLE");

        fil.setFont(new java.awt.Font("Comic Sans MS", 0, 11));

        jLabel3.setFont(new java.awt.Font("Kristen ITC", 0, 14));
        jLabel3.setForeground(new java.awt.Color(153, 0, 153));
        jLabel3.setText("Filas");

        jLabel4.setFont(new java.awt.Font("Kristen ITC", 0, 14));
        jLabel4.setForeground(new java.awt.Color(153, 0, 153));
        jLabel4.setText("Columnas");

        col.setFont(new java.awt.Font("Comic Sans MS", 0, 11));

        jLabel5.setFont(new java.awt.Font("Kristen ITC", 0, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(153, 0, 153));
        jLabel5.setText("Valor");

        valor.setFont(new java.awt.Font("Comic Sans MS", 0, 11));

        mos.setColumns(20);
        mos.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
        mos.setRows(5);
        jScrollPane1.setViewportView(mos);

        guardar.setFont(new java.awt.Font("Kristen ITC", 0, 12)); // NOI18N
        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });

        llenar.setFont(new java.awt.Font("Kristen ITC", 0, 12)); // NOI18N
        llenar.setText("Llenar");
        llenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llenarActionPerformed(evt);
            }
        });

        limpiar.setFont(new java.awt.Font("Kristen ITC", 0, 12));
        limpiar.setText("Limpiar");

        salir.setFont(new java.awt.Font("Kristen ITC", 0, 12));
        salir.setText("Salir");
        salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirActionPerformed(evt);
            }
        });

        crear.setFont(new java.awt.Font("Kristen ITC", 0, 11)); // NOI18N
        crear.setText("Crear");
        crear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addComponent(jLabel1)
                .addContainerGap(196, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(52, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fil, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(col, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(21, 21, 21)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(jLabel5)))))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(guardar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(llenar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(limpiar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(salir)))
                .addGap(31, 31, 31))
            .addGroup(layout.createSequentialGroup()
                .addGap(176, 176, 176)
                .addComponent(crear)
                .addContainerGap(222, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(crear)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fil, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(col, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(guardar))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(llenar)
                        .addGap(18, 18, 18)
                        .addComponent(limpiar)
                        .addGap(26, 26, 26)
                        .addComponent(salir)))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirActionPerformed
JOptionPane.showMessageDialog(rootPane, "DESARROLLADO POR: \n  JORGE SIERRA.", "         CREDITOS", WIDTH);
        dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_salirActionPerformed

    private void crearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearActionPerformed

 mat.fil= Integer.parseInt(JOptionPane.showInputDialog(rootPane,"INGRESE EL NUMERO DE FILAS")); 
 mat.col= Integer.parseInt(JOptionPane.showInputDialog(rootPane,"INGRESE EL NUMERO DE COLUMNAS"));       
mat.crear();
JOptionPane.showMessageDialog(rootPane, "SE HA CREADO LA MATRIZ DE "+mat.fil+ " X "+ mat.col);
mat.llenar();
mostrar(); 
// TODO add your handling code here:
    }//GEN-LAST:event_crearActionPerformed

    private void llenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llenarActionPerformed
mat.LlenarAleatorio();
Mos="";
mostrar();
        // TODO add your handling code here:
    }//GEN-LAST:event_llenarActionPerformed

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed
if (fil.getValue()==0 && col.getValue()==0) {
Mos="";
double val = Double.parseDouble(valor.getText());
mat.setMatriz((int) fil.getValue(),(int) col.getValue(),val );     
valor.setText("");
fil.setValue((int) fil.getValue()+1);
}
else
if ((int) fil.getValue()<mat.fil  && (int) col.getValue()<mat.col){
    
double val = Double.parseDouble(valor.getText());
mat.setMatriz((int) fil.getValue(),(int) col.getValue(),val );     
valor.setText("");
fil.setValue((int) fil.getValue()+1);

if ((int) fil.getValue()==mat.fil && (int) col.getValue()<mat.col){
col.setValue((int) col.getValue()+1);  
fil.setValue((int) fil.getValue()-(int) fil.getValue());
}
}        
else
{
    Mos = "";
    mostrar();
JOptionPane.showMessageDialog(rootPane, "LA MATRIZ ESTA LLENA");
valor.setEnabled(false);
guardar.setEnabled(false);
}          
        // TODO add your handling code here:
    }//GEN-LAST:event_guardarActionPerformed

    public void mostrar(){
    
  for (int f=0;f<mat.fil;f++){
      for (int c=0;c<mat.col;c++){
       Mos+= String.valueOf(mat.getMatriz(f, c))+ "   ";   
      }
    Mos = Mos +"\n";  
  }
    mos.setText(Mos);
    }
    /**
     * @param args the command line arguments
     */
  
 public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            VentanaMatriz ventana = new VentanaMatriz();
            public void run() {
               ventana.setVisible(true);
               ventana.setLocation(400, 200);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner col;
    private javax.swing.JButton crear;
    private javax.swing.JSpinner fil;
    private javax.swing.JButton guardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton limpiar;
    private javax.swing.JButton llenar;
    private javax.swing.JTextArea mos;
    private javax.swing.JButton salir;
    private javax.swing.JTextField valor;
    // End of variables declaration//GEN-END:variables
}
